import { createRouter, createWebHashHistory } from 'vue-router'
import userUtils from "@/utils/userUtils"
import pathUtils from '@/utils/pathUtils'

import Home from '@/views/tabbar/home'
import Login from '@/views/login'

import Index from '@/views/tabbar/index'
import Category from '@/views/tabbar/category'
import Cart from '@/views/tabbar/cart'
import Mine from '@/views/tabbar/mine'

import Search from '@/views/search'
import Product from '@/views/product'

import OrderList from '@/views/order/list'
import OrderDetail from '@/views/order/detail'
import OrderConfirm from '@/views/order/confirm'
import PaySuccess from '@/views/order/success'
import OrderRefund from "@/views/order/refund"
import OrderRefundProcess from "@/views/order/refund/process"
import OrderReturn from "@/views/order/return"
import OrderReturnDelivery from "@/views/order/return/delivery"

import AddressList from '@/views/address/list'
import AddressUpdate from '@/views/address/update'

const routes = [
  {
    path: '/',
    name: 'Home',
    component: Home,
    meta: {
      requiresAuth: false,
    },
    children: [
      {
        path: '',
        name: 'Index',
        component: Index,
        meta: {
          requiresAuth: false,
          title: "首页"
        }
      }, {
        path: '/category',
        name: 'Category',
        component: Category,
        meta: {
          requiresAuth: false,
          title: "分类"
        }
      }, {
        path: '/cart',
        name: 'Cart',
        component: Cart,
        meta: {
          requiresAuth: false,
          title: "购物车"
        }
      }, {
        path: '/mine',
        name: 'Mine',
        component: Mine,
        meta: {
          requiresAuth: false,
          title: "我的"
        }
      }
    ]
  }, {
    path: '/login',
    name: 'Login',
    component: Login,
    meta: {
      requiresAuth: false,
      title: "登录"
    }
  }, {
    path: '/search',
    name: 'Search',
    component: Search,
    meta: {
      requiresAuth: false,
      title: "搜索"
    }
  }, {
    path: '/product',
    name: 'Product',
    component: Product,
    meta: {
      requiresAuth: false,
      title: "商品详情"
    }
  }, {
    path: '/order/list',
    name: 'OrderList',
    component: OrderList,
    meta: {
      requiresAuth: true,
      title: "我的订单"
    }
  }, {
    path: '/order/detail',
    name: 'OrderDetail',
    component: OrderDetail,
    meta: {
      requiresAuth: true,
      title: "订单详情"
    }
  }, {
    path: '/order/confirm',
    name: 'OrderConfirm',
    component: OrderConfirm,
    meta: {
      requiresAuth: true,
      title: "确认订单"
    }
  }, {
    path: '/order/pay/success',
    name: 'PaySuccess',
    component: PaySuccess,
    meta: {
      requiresAuth: true,
      title: "支付成功"
    }
  }, {
    path: '/order/refund',
    name: 'OrderRefund',
    component: OrderRefund,
    meta: {
      requiresAuth: true,
      title: "申请退款"
    }
  }, {
    path: '/order/refund/process',
    name: 'OrderRefundProcess',
    component: OrderRefundProcess,
    meta: {
      requiresAuth: true,
      title: "售前退款进度"
    }
  }, {
    path: '/order/return',
    name: 'OrderReturn',
    component: OrderReturn,
    meta: {
      requiresAuth: true,
      title: "退款退货"
    }
  }, {
    path: '/order/return/delivery',
    name: 'OrderReturnDelivery',
    component: OrderReturnDelivery,
    meta: {
      requiresAuth: true,
      title: "退货发货"
    }
  }, {
    path: '/address/list',
    name: 'AddressList',
    component: AddressList,
    meta: {
      requiresAuth: true,
      title: "收货地址"
    }
  }, {
    path: '/address/update',
    name: 'AddressUpdate',
    component: AddressUpdate,
    meta: {
      requiresAuth: true,
      title: "编辑收货地址"
    }
  }
]

const router = createRouter({
  history: createWebHashHistory(),
  routes
})

router.beforeEach((to, from, next) => {
  if (to.name == "OrderConfirm" && from.name) {
    pathUtils.setOrderConfirmPrevRoute(from);
  }
  if (to.name == "OrderConfirm" && !from.name) {
    var prevRoute = pathUtils.getOrderConfirmPrevRoute();
    if (null != prevRoute) {
      next({ path: prevRoute.fullPath, query: prevRoute.query, params: prevRoute.params })
    } else {
      next({ path: "/" })
    }
    return;
  }

  if (to.meta.requiresAuth) {
    userUtils.isLogin().then(res => {
      if (res.data) {
        next()
      } else {
        // 未登录，跳转到登录页面
        next({ path: '/login' })
      }
    })

  } else {
    next()
  }
})

export default router
